package com.yanqu.road.server.manger.activity.musicroomrank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.musicroomrank.MusicRoomRankConfigRankData;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankBeautyData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.musicroomrank.MusicRoomRankConfig;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.pb.activity.MusicRoomRankProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.musicroom.MusicRoomRankActivityModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MusicRoomRankActivityMgr extends TempMgr {
    private static MusicRoomRankConfig config = null;
    private static ThreadTaskManger threadTaskManger;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        if (null == threadTaskManger) {
            threadTaskManger = new ThreadTaskManger(1, "MusicRoomRankActivityMgr");
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivity() {
        getLogger().info("reload MusicRoomRankActivityMgr start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.MusicRoomRank.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no MusicRoomRankActivityMgr Activity in show time");
            config = null;
        } else {
            ActivityInfo tempActivityInfo = openActivityInfoList.get(0);
            MusicRoomRankConfig tempConfig = new MusicRoomRankConfig(tempActivityInfo);
            config = tempConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(MusicRoomRankActivityModule.class).initUserData();
            player.getModule(MusicRoomRankActivityModule.class).syncConfig();
            player.getModule(MusicRoomRankActivityModule.class).syncUserData();
        }
        getLogger().info("reload MusicRoomRankActivityMgr end");
    }

    public static MusicRoomRankConfig getConfig() {
        return config;
    }

    public static boolean isInActivityTime() {
        if (config == null) {
            return false;
        }
        return ActivityMgr.activityInTime(config.getActivityInfo());
    }

    public static boolean isInActivityShow() {
        if (config == null) {
            return false;
        }
        return ActivityMgr.activityInShowTime(config.getActivityInfo());
    }

    public static int getActivityId() {
        if (config == null) {
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }

    public static MusicRoomRankProto.MusicRoomRankConfigMsg.Builder getConfigMsg(String language) {
        MusicRoomRankProto.MusicRoomRankConfigMsg.Builder builder = MusicRoomRankProto.MusicRoomRankConfigMsg.newBuilder();
        builder.setActivityId(getActivityId());

        for (MusicRoomRankConfigRankData configRankData : config.getRankConfigList()) {
            MusicRoomRankProto.MusicRoomRankConfigTemp.Builder temp = MusicRoomRankProto.MusicRoomRankConfigTemp.newBuilder();
            temp.setId(configRankData.getId());
            temp.setName(ServerLanguageMgr.getContent(configRankData.getName(), language));
            temp.setRank(configRankData.getRank());
            temp.setScore(configRankData.getScore());
            temp.setReward(configRankData.getReward());
            temp.setDesc(ServerLanguageMgr.getContent(configRankData.getDesc(), language));
            temp.setIcon(configRankData.getIcon());
            builder.addConfigData(temp);
        }
        return builder;
    }

    public static void addTask(int round, int theme, Map<Long, List<MusicRoomRankProto.MusicRoomRankShowTemp>> userResultMap) {
        if (threadTaskManger != null) {
            threadTaskManger.addTask(0, new MusicRoomRankActivityTask(round, theme, userResultMap));
        }
    }
}
